﻿using UnityEngine;
using System.Collections;

public class CharacterSkill : MonoBehaviour {
	public GameObject attackObj;

	//objects for referencing
	SpriteRenderer _spriteRendererForAttackObj;
	BoxCollider2D _colliderForAttackObj;

	//other local variables
	bool isKeyCombinationActivated = false;
	int totalKeyCombined = 0;
	float keyCombination = 0f;
	float durationForKeyCombination = 3f;
	const float durationForKeyCombination_default = 3f;


	void Start () {
		_spriteRendererForAttackObj = attackObj.GetComponent<SpriteRenderer>();
		_colliderForAttackObj = attackObj.GetComponent<BoxCollider2D>();
		Debug.Log(Mathf.Pow(0.1f,1));
		Debug.Log(Mathf.Pow(0.1f,2));
	}
	

	void Update () {
		if(Input.GetButtonDown("Fire3")){
			isKeyCombinationActivated = true;
		}else if(Input.GetButtonUp("Fire3")){
			resetKeyCombinationDetails();
		}


		if(isKeyCombinationActivated){
			durationForKeyCombination -= Time.deltaTime;

			if(durationForKeyCombination <= 0f){
				//if duration time out, then we will stop "recording" key combination
				resetKeyCombinationDetails();
			}else{
				//otherwise, continue "recording" key combination
				catchAllPressedKey();
			}
		}

	}// Update()

	void catchAllPressedKey(){
		int pressedKey = 0;

		//use else if, because we don't allow multiple key pressed at the same time
		if( Input.GetKeyDown(KeyCode.UpArrow) ){
			pressedKey = 1;
		}else if( Input.GetKeyDown(KeyCode.DownArrow) ){
			pressedKey = 2;
		}else if( Input.GetKeyDown(KeyCode.LeftArrow) ){
			pressedKey = 3;
		}else if( Input.GetKeyDown(KeyCode.RightArrow) ){
			pressedKey = 4;
		}

		if(pressedKey > 0){
			totalKeyCombined++;
			float amountToPower = (Mathf.Pow(0.1f,totalKeyCombined));
			keyCombination += (amountToPower*pressedKey);
		}
	}

	public void resetKeyCombinationDetails(){
		Debug.Log("Key Combination reset, combination: " + keyCombination);
		//reset key combination
		durationForKeyCombination = durationForKeyCombination_default;
		keyCombination = 0f;
		totalKeyCombined = 0;
		isKeyCombinationActivated = false;
	}

	public bool ifKeyCombinationIsActivated(){
		return isKeyCombinationActivated;
	}

	public float getKeyCombination(){
		return keyCombination;
	}
}
